﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Services;

/// <summary>
/// Public webservice to upload and publish score data.
/// </summary>
[WebService( Namespace = "http://www.codeplex.com/Hero" )]
[WebServiceBinding( ConformsTo = WsiProfiles.BasicProfile1_1 )]
public class HeroService : WebService
{

	/// <summary>
	/// Saves a new score record to the database.
	/// </summary>
	/// <param name="userName">The user's nick name.</param>
	/// <param name="score">The achieved score.</param>
	/// <param name="playDate">The date when the user played the game.</param>
	/// <param name="songId">The identifier of the song.</param>
	/// <param name="difficulty">The difficulty level of the game.</param>
	[WebMethod( Description = "Saves a new score record to the database."  )]
	public void UploadResult( string userName, int score, DateTime playDate, int songId, int difficulty )
	{
		Result result = new Result{
			UserName = userName,
			Score = score,
			PlayDate = playDate,
			SongID = songId,
			Difficulty = difficulty
		};
		
		HeroDataContext dc = new HeroDataContext();
		dc.Results.InsertOnSubmit( result );
		dc.SubmitChanges();
	}


	/// <summary>
	/// Returns the top results for the specified song with the specified difficulty level.
	/// </summary>
	/// <param name="count">The number of score records to be returned.</param>
	/// <param name="songId">The unique identifier of the song used to filter results.</param>
	/// <param name="difficulty">The difficulty level used to filter results.</param>
	/// <returns>A <c>Result</c> list.</returns>
	[WebMethod( Description = "Returns the top results for the specified song with the specified difficulty level." )]
	public List<Result> GetTopResults( int count, int songId, int difficulty )
	{
		HeroDataContext dc = new HeroDataContext();
		return dc.Results.Where( r => r.SongID == songId && r.Difficulty == difficulty ).OrderByDescending( r => r.Score ).Take( count ).ToList();
	}

}

